package com.myway.controller;

import com.myway.dto.common.JsonData;
import com.myway.dto.param.HotelParam;
import com.myway.dto.param.HouseParam;
import com.myway.pojo.TabOrderHotel;
import com.myway.service.HotelService;
import com.myway.utils.DataGridResult;
import com.myway.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/en/hotel")
public class HotelControllerEn {

    @Autowired
    private HotelService hotelService;

    /**
     *
     * js调用:hotelhome.js
     * @return 热门酒店城市列表
     */
    @RequestMapping("/hotcharact.json")
    @ResponseBody
    public JsonData selectHotHotelCity(){
        return hotelService.selectHotHotelCity();
    }

    /**
     *js调用:hotelhome.js
     * @param param
     * @return 热门目的地酒店列表
     */
    @RequestMapping("/hotelcharact.json")
    @ResponseBody
    public JsonData  selectHotelHomeCharact(HotelParam param) {
        return hotelService.selectHotelHomeCharact(param);
    }


    /**
     * hotelhome.jsp页面跳转的查询条件
     */

    @RequestMapping("/hotelcity.json")
    public String selectHotelCity(HotelParam param,Model model){
        model.addAttribute("param",param);
        return "en/hotel";
    }


    /**
     * 分页条件查询
     * @param param
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/hotellist.json")
    @ResponseBody
    public DataGridResult selectHotel(HotelParam param,@RequestParam(defaultValue = "0",value = "page") Integer page,
                                      @RequestParam(defaultValue = "5",value = "rows") Integer rows){
        DataGridResult result = hotelService.selectHotelInf(param,page,rows);
        return  result;
    }

    /**
     * 酒店详情列表
     * @param hotelId
     * @param model
     * @return
     */
    @RequestMapping("/houseinf.json/{hotelId}")
    public String  selectHouseInf(@PathVariable(value = "hotelId") String hotelId,Model model){
        HashMap map = hotelService.selectHouseInf(hotelId);
        model.addAttribute("map",map);
        return "en/hotelDetail";
    }

    @RequestMapping("/housedate.json")
    @ResponseBody
    public JsonData selectHouseDate(HouseParam param){
        if(param.getStartTime()==null||param.getEndTime()==null){
            param.setStartTime(new Date());
//            param.setEndTime(DateUtils.addOneDay(new Date()));
        }
        return hotelService.selectUseHouse(param);
    }

    /**
     * 跳转订单详情页面，如果未登录，前往登录，登录则继续进行下一步
     * @param houseId
     * @param startTime
     * @param endTime
     * @param model
     * @return
     */
    @RequestMapping("/houselist.json" )
    public String  selectHouseList(HttpServletRequest request,Integer houseId, Date startTime,Date endTime,Model model){
        String str =(String)request.getSession().getAttribute("userType");
        if(str !=null){
            Map map= hotelService.selectReseveHouse(houseId);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("houseid",houseId);
            map.put("dateCha",(int) ((endTime.getTime() - startTime.getTime()) / (1000*3600*24)));
            model.addAttribute("map",map);
            return  "en/subOrder";
        }else{
            return "en/login";
        }

    }

    /**
     * 验证酒店房间数是否足够
     * @param id
     * @param num
     * @return
     */
    @RequestMapping("/housenum.json" )
    @ResponseBody
    public JsonData selectHouseNum(Integer id, Integer num){
       return hotelService.selectHouseNum(id,num);
    }

    /**
     * 酒店订单生成，并修改酒店预售房间等相关信息。
     * @param orderhotel
     * @param city
     * @return
     */
    @RequestMapping("/houseorder.json" )
    @ResponseBody
    public JsonData addHouseOrder(TabOrderHotel orderhotel, String city){
        return  hotelService.addHouseOrder(orderhotel,city);
    }

}
